﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using Flagwind.Resources;

namespace Flagwind.Services.Commands
{
	[DisplayName("${Text.ServicesCommand.DisplayName}")]
	[Description("${Text.ServicesCommand.Description}")]
	[CommandOption("provider", typeof(string), Description = "${Text.ServicesCommand.Options.Provider}")]
	public class ServicesCommand : CommandBase<CommandContext>
	{
		#region 成员字段

		private IServiceProvider _serviceProvider;
		private IServiceProviderFactory _serviceFactory;

		#endregion

		#region 构造方法

		protected ServicesCommand() : base("Services")
		{
		}

		protected ServicesCommand(string name) : base(name)
		{
		}

		public ServicesCommand(IServiceProviderFactory serviceFactory) : this("Services", serviceFactory)
		{
		}

		protected ServicesCommand(string name, IServiceProviderFactory serviceFactory) : base(name)
		{
			if(serviceFactory == null)
			{
				throw new ArgumentNullException("providerFactory");
			}

			_serviceFactory = serviceFactory;
			_serviceProvider = serviceFactory.Default;
		}

		#endregion

		#region 公共属性

		public IServiceProviderFactory ServiceFactory
		{
			get
			{
				return _serviceFactory;
			}
		}

		public Flagwind.Services.IServiceProvider ServiceProvider
		{
			get
			{
				return _serviceProvider;
			}
			set
			{
				_serviceProvider = value;
			}
		}

		#endregion

		#region 重写方法

		protected override object OnExecute(CommandContext context)
		{
			if(_serviceFactory == null)
			{
				throw new CommandException(ResourceUtility.GetString("Text.CannotObtainCommandTarget", "ServiceProviderFactory"));
			}

			string providerName;

			if(context.Expression.Options.TryGetValue("provider", out providerName))
			{
				if(string.IsNullOrWhiteSpace(providerName) || providerName == "~" || providerName == ".")
				{
					_serviceProvider = _serviceFactory.Default;
				}
				else
				{
					var provider = _serviceFactory.GetProvider(providerName);

					if(provider == null)
					{
						throw new CommandException(ResourceUtility.GetString("Text.ServicesCommand.NotFoundProvider", providerName));
					}

					_serviceProvider = provider;
				}

				//显示执行成功的信息
				context.Output.WriteLine(ResourceUtility.GetString("Text.CommandExecuteSucceed"));
			}

			var items = _serviceFactory as IEnumerable<KeyValuePair<string, Flagwind.Services.IServiceProvider>>;

			if(items != null)
			{
				int index = 1;

				foreach(var item in items)
				{
					context.Output.Write(CommandOutletColor.DarkMagenta, "[{0}] ", index++);

					if(string.IsNullOrWhiteSpace(item.Key))
					{
						context.Output.Write("<Default>");
					}
					else
					{
						context.Output.Write(item.Key);
					}

					if(object.ReferenceEquals(item.Value, this.ServiceProvider))
					{
						context.Output.WriteLine(CommandOutletColor.Green, " (Actived)");
					}
					else
					{
						context.Output.WriteLine();
					}
				}
			}

			return _serviceProvider;
		}

		#endregion

		#region 静态方法

		internal static Flagwind.Services.IServiceProvider GetServiceProvider(CommandTreeNode node)
		{
			if(node == null)
			{
				return null;
			}

			var command = node.Command as ServicesCommand;

			if(command != null)
			{
				return command.ServiceProvider;
			}

			return GetServiceProvider(node.Parent);
		}

		#endregion
	}
}